var express = require('express');
var router = express.Router();
var dataCtrl = require('../controllers/user.controllers');
var jwt = require('jsonwebtoken');

function verifyToken(req,res,next){
    const bearerHeader=req.headers["authorization"];
    console.log(bearerHeader)
   if(typeof bearerHeader !=="undefined"){
     const bearer=bearerHeader.split(" ");
     const bearerToken=bearer[1];

     jwt.verify(bearerToken, 'mySecretString', function(err, decoded) {
         if(decoded!==undefined){
             next();
         }else{
             res.sendStatus(403);
         }

      });

   }else{
       res.sendStatus(403);
   }
}
router.post('/', dataCtrl.create);
router.put('/:id', dataCtrl.update);
router.delete('/:id',verifyToken,dataCtrl.remove);
router.post('/list', dataCtrl.list);
router.get('/:id', dataCtrl.get);
router.post('/removes', dataCtrl.removes);
router.post('/login', dataCtrl.login);

module.exports = router;